package com.niit.controller;

import com.github.pagehelper.PageInfo;
import com.niit.entity.Student;
import com.niit.service.StudentService;
import com.niit.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @GetMapping("/student/{id}")
    public Result<Student> getStudent(@PathVariable("id") int id){
        Student student = studentService.findOneById(id);
        if (student != null){
            return Result.success("200","查询成功",student);
        }else {
            return  Result.error("202", "查询失败");
        }
    }

    @GetMapping("/byPage")
    public  Result findAllByPage(Student student,int pageNum,int pageSize){
        System.out.println("student:"+student);
        System.out.println("pageNum:"+pageNum+"pageSize:"+pageSize);
        PageInfo pageInfo = studentService.findAllByPage(student,pageNum,pageSize);
        System.out.println("分页信息："+pageInfo);
        return Result.success(pageInfo);
    }

    @PostMapping("/student")
    public Result addStudent(Student student){
        int rows = studentService.saveStudent(student);
        if (rows > 0){
            return Result.success("200","新增学生信息成功",student);
        }else {
            return Result.error("202", "新增学生信息失败");
        }
    }

    @PutMapping("/student")
    public Result editStudent(Student student){
        int rows = studentService.updateStudent(student);
        if (rows > 0){
            return Result.success("200","修改学生信息成功",student);
        }else {
            return Result.error("202", "修改学生信息失败");
        }
    }

    @DeleteMapping("/student/{id}")
    public  Result remove(@PathVariable("id") int id){
        int rows = studentService.deleteStudent(id);
        if (rows > 0){
            return Result.success("200","删除学生信息成功",rows);
        }else {
            return Result.error("202", "删除学生信息失败");
        }
    }
}




















